package com.sgb.kjwl.view.ui.activity.enterprise;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;

import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityNeedSignupdetailBinding;
import com.sgb.kjwl.databinding.LayoutNeedmessageBinding;
import com.sgb.kjwl.model.entity.NeedDetailsEntity;
import com.sgb.kjwl.model.entity.enterprise.NeedDetailSignUpEntity;
import com.sgb.kjwl.utils.ActivityJump;
import com.sgb.kjwl.utils.DialogFunction;
import com.sgb.kjwl.utils.PointUtils;
import com.sgb.kjwl.viewmodel.enterprise.NeedSignUpListViewModel;
import com.sgb.kjwl.viewmodel.enterprise.SignUpDetailViewModel;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.io.Serializable;

/**
 * 报价详情
 */
public class SignUpDetailActivity extends AppActivity {
    private ActivityNeedSignupdetailBinding mBinding;
    private SignUpDetailViewModel mViewModel;


    @Override
    protected void initView() {
        super.initView();

        mViewModel = new ViewModelProvider(this).get(SignUpDetailViewModel.class);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_need_signupdetail);
        mBinding.setVariable(BR.signupmodel, mViewModel);
        mBinding.setLifecycleOwner(this);

        mViewModel.mItemData = (NeedDetailSignUpEntity) getIntent().getSerializableExtra("data");
        mViewModel.needData = (NeedDetailsEntity) getIntent().getSerializableExtra("needData");
        if (mViewModel.needData != null)
            mBinding.tvChoice.setVisibility(mViewModel.needData.getOfferStatus() == 1 ? View.VISIBLE : View.GONE);
        initObServe();
        initLister();
        mViewModel.getDetail();
    }


    private void initLister() {
        mBinding.tvChoice.setOnClickListener(view -> {
            PublishPublicityActivity.start(SignUpDetailActivity.this, mViewModel.data.getValue(), mViewModel.needData);//设置公示
            finish();
        });

        mBinding.tvPhone.setOnClickListener(view -> {
            final String phone = ((TextView) view).getText().toString();
            com.sgb.kjwl.utils.DialogHelper.showDialog(this, "确认呼叫", phone, "确定", new DialogFunction() {
                @Override
                public void normalRoundDialog() {
                    ActivityJump.jumpToPhone(SignUpDetailActivity.this, phone);
                }
            });
        });
    }


    private void initObServe() {
        mViewModel.ToastShow.observe(this, data -> {
            MToast.showLongToast(this, data);
        });
        mViewModel.showloading.observe(this, Integer -> {
            switch (Integer) {
                case 1:
                    DialogHelper.showProgressDialog(SignUpDetailActivity.this, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
                    break;
                case 2:
                    DialogHelper.dismissProgressDialog();
                    break;
            }
        });

        mViewModel.data.observe(this, data -> {
            if (data.getPrice() > -1) {
                mBinding.tvPrice.setText("￥" + PointUtils.price(data.getPrice()));
            } else {
                mBinding.tvPrice.setText("议价");
            }

            if (!TextUtils.isEmpty(data.getDemandAttachment()) && !"{}".equals(data.getDemandAttachment())) {
                mBinding.linCode.setVisibility(View.VISIBLE);

                JSONObject jsonObject = JSONObject.parseObject(data.getDemandAttachment());
                if (null != jsonObject.get("name")) {
                    String name = jsonObject.getString("name");
                    mBinding.tvName.setText(name);
                }
                if (null != jsonObject.get("size")) {
                    mBinding.tvSize.setText(jsonObject.getString("size"));
                }
                if (jsonObject.get("type") != null) {
                    String fileType = jsonObject.getString("type");

                    switch (fileType) {
                        case "application/zip":
                        case "application/rar":
                            Glide.with(SignUpDetailActivity.this).load(R.mipmap.icon_zip).into(mBinding.image);
                            break;
                        case "image/png":
                        case "image/jpeg":
                            Glide.with(SignUpDetailActivity.this).load(R.mipmap.icon_image).into(mBinding.image);
                            break;
                        case "application/pdf":
                            Glide.with(SignUpDetailActivity.this).load(R.mipmap.icon_pdf2).into(mBinding.image);
                            break;
                    }
                }

            } else {
                mBinding.linCode.setVisibility(View.GONE);
            }

        });
    }

    public static void start(Context mContext, NeedDetailSignUpEntity data, NeedDetailsEntity needData) {
        Intent mIntent = new Intent(mContext, SignUpDetailActivity.class);
        mIntent.putExtra("data", (Serializable) data);
        mIntent.putExtra("needData", (Serializable) needData);
        mContext.startActivity(mIntent);
    }

}
